package com.example.pcbcsmbackend.repository;

import com.example.pcbcsmbackend.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface OrderRepository extends JpaRepository<Order, Integer> {
    // 根据用户ID查找订单
    List<Order> findByUserId(Integer userId);
    
    // 获取所有订单，并按购买时间倒序排序
    List<Order> findAllByOrderByPurchaseTimeDesc();
    
    // 根据订单状态查找订单
    List<Order> findByOrderStatus(String orderStatus);
    
    // 根据用户ID和订单状态查找订单
    List<Order> findByUserIdAndOrderStatus(Integer userId, String orderStatus);
    
    // 根据用户ID按购买时间倒序查找订单
    List<Order> findByUserIdOrderByPurchaseTimeDesc(Integer userId);
    
    // 根据用户ID查找订单ID列表
    @Query("SELECT o.orderId FROM Order o WHERE o.userId = :userId")
    List<Integer> findOrderIdsByUserId(@Param("userId") Integer userId);
    
    // 根据用户ID删除订单
    @Transactional
    void deleteByUserId(Integer userId);
} 